const app = getApp()
const api = require('../../../config/api.js')
Page({
  data: {
    genders:["保密","小哥哥","小姐姐"]

  },

  getUserInfo:function(){
    let userInfo = {}
    if (app.globalData.xuserInfo) {
      userInfo = Object.assign(app.globalData.userInfo,app.globalData.xuserInfo)
    } else {
      userInfo = app.globalData.userInfo
    }
    return userInfo
  },

  onLoad() {
    let userInfo = this.getUserInfo()
    this.setData({
      avatarUrl:userInfo.avatarUrl,
      nickName:userInfo.nickName,
      gender:userInfo.gender,
      age:userInfo.age,
      constellation:userInfo.constellation,
      location:userInfo.location?userInfo.location:" ",
      labels:userInfo.labels,
      words:userInfo.words
    })
  },
  onShow() {
    let userInfo = this.getUserInfo()
    this.setData({
      avatarUrl:userInfo.avatarUrl,
      nickName:userInfo.nickName,
      gender:userInfo.gender,
      age:userInfo.age,
      constellation:userInfo.constellation,
      location:userInfo.location?userInfo.location:" ",
      labels:userInfo.labels,
      words:userInfo.words
    })
  },
  onUnload:function(){
    qq.switchTab({
      url: "/pages/mine/mine"
    })
  },

  toNickname:function(e){
    console.log("change name")
    qq.navigateTo({
      url: './nickname/nickname',
    })
  },

  bindAgeChange: function(e) {
    birthdate=e.detail.value.split("-")
    brithyear=Number(birthdate[0])
    brithmonth=Number(birthdate[1])
    brithday=Number(birthdate[2])
    nowday = new Date().getDate()
    nowmonth = new Date().getMonth()+1
    nowyear = new Date().getFullYear()
    if(brithyear<=nowyear){
      age=nowyear-brithyear
      if(brithmonth>nowmonth){
        age=age-1
      }
      else if(brithmonth===nowmonth){
        if(brithday>nowday){
          age=age-1
        }
      }
    }
    else{
      age=0
    }
    //根据生日计算星座
    let s="魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
    let arr=[20,19,21,21,21,22,23,23,23,23,22,22];
    let constellation = s.substr(brithmonth*2-(brithday<arr[brithmonth-1]?2:0),2);
    this.updataXUserInfo(["age","constellation"],[age,constellation])
    this.setData({
      age: age,
      constellation:constellation
    })
    let pages = getCurrentPages()  //获取当前页面栈的信息
    let prevPage1 = pages[pages.length - 2]   //获取上一个页面
    prevPage1.setData({   //把需要回传的值保存到上一个页面
      age: age,
      constellation:constellation
    });
  },
  bindCityChange: function (e) {
    let evalue = e.detail.value
    let province = evalue[0]
    let city = evalue[1]
    let district = evalue[2]
    let location = province+" "+city
    this.updataXUserInfo(["location","province","city","district"],[location,province,city,district])
    this.setData({
      location: location
    })
  },
  bindGenderChange: function(e) {
    let gender = Number(e.detail.value)
    this.updataXUserInfo(["gender"],[gender])
    this.setData({
      gender: gender
    })
  },
  toChangesign:function(e){
    console.log("change signature")
    qq.navigateTo({
      url: './changesign/changesign',
    })
  },
  toPersonalfiles:function(e){
    console.log("change name")
    qq.navigateTo({
      url: './loveTa/loveTa',
    })
  },
  toAboutMe:function(){
    qq.navigateTo({
      url: './aboutMe/aboutMe',
    })
  },



  
  updataXUserInfo:function(xuserinfokeys,xuserinfovalues){
    let xuserInfo = {}
    for(i=0;i<xuserinfokeys.length;i++){
      xuserInfo[xuserinfokeys[i]] = xuserinfovalues[i]
    }
    qq.request({
      url: api.SyncXUserInfo,
      data: {"xuserInfo":xuserInfo},
      header: {
        'Content-Type': 'application/json',
        'Xworld-Token': app.globalData.token
      },
      method: 'POST',
      success: res => {
        app.globalData.xuserInfo = res.data.data.xuserInfo
        console.log('res success qq.getXUserInfo', res)

      },  
      fail: res => {
        console.log('res fail', res)
      }
    })
  },
  backtoprepage:function(e){
    //跳转tabbar界面要用switchTab
    qq.switchTab({
      url: '/pages/mine/mine'
    })
  }

})